<?php 
namespace Home\Controller;
class MycommentController extends HomeController
{

    public function count(){
        $user_id = $_SESSION['user']['id'];
        // 获取评论的条数
        $comment_count = M('comment')->where(array('user_id'=>$user_id,'status'=>'1'))->count();
        $remark_count = M('reply_comments')->where(array('user_id'=>$user_id))->count();
        $this->assign('comment_count',$comment_count);
        $this->assign('remark_count',$remark_count);
    }
    public function comment()
    {

    $_GET['p'] = empty($_GET['p'])?'1':$_GET['p'];
    $user_id = $_SESSION['user']['id'];
    $m = M('comment');
    $list = $m->field('c.c_content,c.com_time,a.title,a.id,c.id c_id')
                    ->table('hx_comment c,hx_article a')
                    ->where("c.user_id=$user_id AND c.status=1 AND a.id=c.art_id")
                    ->order('c.id DESC')
                    ->page($_GET['p'].',5')
                    ->select();


    $count = $m->table('hx_comment c,hx_article a')
                         ->where("c.user_id=$user_id AND c.status=1 AND a.id=c.art_id")->count();
    $p=getpage($m,$count,5);
    $this->list=$list;
    $this->assign('list',$list);// 赋值数据集
    $this->page=$p->show();


    // $user_id = $_SESSION['user']['id'];
    // $info = M('comment')->where(array('user_id'=>$user_id,'status'=>'1'))->select();

    //     $i = 0;
    // foreach ($info as $key => $v) {
    //     $i++;
    //     $_SESSION['comment'][$i]=$v;
    //     $id=$v['art_id'];
    //     $title = M('article')->where(array('id'=>$id))->field('title')->select();
    //     foreach ($title['0'] as  $val) {
    //         $title =$val;
    //     }
    //     $_SESSION['comment'][$i]['title'] =$title;

    // }


        $this->count();

    $this->display('Mycomment/comment');
    }

    public function del_comment()
    {
        V($_GET);
        $id = I('get.id/d');
        $data = M('comment');
       $info =  $data->where(array('id'=>$id))->delete();
        if ($info) {
            $this->redirect('comment');
        }
    }

    public function remark()
    {

        $user_id = $_SESSION['user']['id'];
        $info = M('reply_comments')->where(array('user_id'=>$user_id))->select();
        // $remark_count = M('reply_comments')->where(array('user_id'=>$user_id))->count();
            $i = 0;
        foreach ($info as $key => $v) {
            $i++;
            $_SESSION['reply_comments'][$i]=$v;
            $id=$v['rep_id'];
            $data = M('comment')->where(array('id'=>$id,'status'=>'1'))->field('c_content,art_id')->select();
            $c_content=$data['0']['c_content'];
            $art_id =$data['0']['art_id'];
            $_SESSION['reply_comments'][$i]['c_content'] = $c_content;
            // 查找评论对应的文章标题
            $title = M('article')->where(array('id'=>$art_id))->field('title')->select();
            $_SESSION['reply_comments'][$i]['title'] = $title['0']['title'];
            // $_SESSION['remark_count'] = $remark_count;

            // V($_SESSION['reply_comments']);
            // $_SESSION['reply_comments'][$i]['title'] =$title;
            }
            $this->count();


        $this->display('Mycomment/remark');
    }


}

